package simtester.agent

import geb.*
import groovyx.net.http.ContentType

class AgentScript {
    void runtest(args) {
        def log = args.logger
        
        def sutAddr = 'http://www.google.com/'
        //def sutAddr = 'http://ec2-50-18-140-125.us-west-1.compute.amazonaws.com:3000/'
        def sut = new RESTCli(args, sutAddr, ContentType.HTML)
        
        Browser.drive {
            clearCookies()
            go sutAddr

            //----------------
            /*sut.get(path: '')
            log.info("Title: " + $('h1').text())
            log.info("Signing in...")
            sut.capture('users/sign_in') { $('a', text: 'Sign in').click() }
            
            $('#new_user').find("input", name: 'user[email]').value('my@user.com')
            $('#new_user').find("input", name: 'user[password]').value('password')
            sut.capture('users/sign_in', '') { $('#new_user').commit().click() }
            
            log.info("Flash: " + $('.flash_notice').text())
            log.info("Go to General Discussion")
            sut.capture('forums/1') {
                $('a.title', text: 'General Discussion').click()
            }
            
            def str = "Existing posts:\n"
            $('table tbody tr').each {
                str += it.find('div.subject').text() + "\n"
            }
            log.info(str)
            
            log.info('Click on New topic')
            sut.capture('forums/1/topics') {
                $('menu#forum_topic_links a', text: 'New topic').click()
            }
            
            log.info("Create topic with...\nSubject: Test Topic\nText: \"Some test text goes here\"")
            $('input#topic_subject', name: 'topic[subject]').value('Test Topic')
            $('textarea').value('Some test text goes here')
            sut.capture('forums/1/topics', 'forums/1/topics') {
                $('input', name: 'commit').click()
            }
            
            log.info("Flash: " + $('.flash_notice').text())
            sut.capture('forums/1') {
                $('h2 a', text: 'General Discussion').click()
            }
            
            str = "Existing posts:\n"
            $('table tbody tr').each {
                str += it.find('div.subject').text() + "\n"
            }
            log.info(str)
            
            assert $('table tbody tr').any { it.find('div.subject').text() == 'Test Topic' }
            
            log.info('Deleting Test Topic')
            sut.capture('forums/1/topics') {
                $('div.subject a', text: 'Test Topic').click()
            }
            
            sut.capture('forums/1/topics', 'forums/1/topics') {
                assert withConfirm(true) { $('menu a', text: 'Delete').click() }
            }
            log.info("Flash: " + $('.flash_notice').text())
            
            str = "Existing posts:\n"
            $('table tbody tr').each {
                str += it.find('div.subject').text() + "\n"
            }
            log.info(str)*/
            //---------------

            log.info("Title: $title")
            assert title == 'Google'

            10.times { sut.fakeget(path: '') }
        }
    }
}
